using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace BTS_Utilities.EDI_Fixup.Availity
{
    public static class FixPayerID
    {
        /// <summary>
        /// This returns the Availity friendly PayerID for Realtime communication
        /// </summary>
        /// <param name="NumericPayerID"></param>
        /// <returns></returns>
        internal static string ConvertPayerIDForRealtimeCommunication(string NumericPayerID)
        {
            switch (NumericPayerID)
            {
                case "00090":
                    return "BCBSF";

                case "61101":
                    return "HUMANA";

                case "60054":
                    return "AETNA";

                case "62308":
                    return "CIGNA";

                case "59321":
                    return "FHHS";

                default:
                    return NumericPayerID;

            }
        }
        /// <summary>
        /// This is used to reset the payer ID to a Alpha value for realtime sending to Availity
        /// if we receive 00090 it needs to be converted to BCBSF
        /// </summary>
        /// <param name="XPathToPayerID"></param>
        /// <param name="document"></param>
        public static XmlDocument SetPayerIDForRealtimeCommunication(string DocumentType, XmlDocument document)
        {
            string XPATH_to_Payer = "";
            if (DocumentType == "837I")
            {
                XPATH_to_Payer = @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2010BC_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='NM1_PayerName_TS837Q3_2010BC' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='NM109__PayerIdentifier' and namespace-uri()='']";
            }
            if (DocumentType == "270")
            {
                XPATH_to_Payer = @"/*[local-name()='X12_00401_270' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS270A1_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS270A1_2100A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='NM1_InformationSourceName_TS270A1_2100A' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='NM109__InformationSourcePrimaryIdentifier' and namespace-uri()='']";
            }
            XmlNodeList nodes = document.SelectNodes(XPATH_to_Payer);
            foreach (XmlNode node in nodes)
            {
                node.InnerText = ConvertPayerIDForRealtimeCommunication(node.InnerText);
            }
            return document;
        }
        public static string GetPayerID(string DocumentType, XmlDocument document)
        {
            string XPATH_to_Payer = "";
            if (DocumentType == "837I")
            {
                XPATH_to_Payer = @"/*[local-name()='X12_00401_837_I' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2000B_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS837Q3_2010BC_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='NM1_PayerName_TS837Q3_2010BC' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='NM109__PayerIdentifier' and namespace-uri()='']";
            }
            if (DocumentType == "270")
            {
                XPATH_to_Payer = @"/*[local-name()='X12_00401_270' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS270A1_2000A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='TS270A1_2100A_Loop' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='NM1_InformationSourceName_TS270A1_2100A' and namespace-uri()='http://schemas.microsoft.com/BizTalk/EDI/X12/2006']/*[local-name()='NM109__InformationSourcePrimaryIdentifier' and namespace-uri()='']";
            }
            XmlNodeList nodes = document.SelectNodes(XPATH_to_Payer);
            //This method is predominantly used in the Availity HTTP Send where there is only one payer hence the zero based array use
            // could use the selectSingleNode but decided to keep this method similar tot he SetPayerID method.
            return nodes[0].InnerText;
            
        }
    }
}
